package ecustom.common.conn;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import ecustom.common.CustomMap;
import ecustom.common.util.PropUtil;
import weaver.conn.WeaverConnection;

/**
 * 数据库连接公共类。
 * 法律声明：非公开代码，仅供授权机构（公司或个人）使用，公开发表将追究法律责任。
 * @author 曹水涛
 */
public abstract class DB {
	
	protected Logger log = Logger.getLogger(getClass());
	
	public static DB conn() {
		return conn("");
	}
	
	public static DB conn(String dsName) {
		String dbEnv = PropUtil.getString("evn.db.mode");
		if ("agent".equalsIgnoreCase(dbEnv))
			return DBAgent.conn(dsName);
		
		return DBServer.conn(dsName);
	}

	public abstract CustomMap keyMap(String keyField, String sql, Object... whereValues) throws Exception;

	public abstract CustomMap one(String sql, Object... values) throws Exception;
	
	public abstract List<CustomMap> list(StringBuilder sql, Object... values) throws Exception;

	public abstract List<CustomMap> list(String sql, Object... values) throws Exception;
	
	public abstract List<String> getStringList(String sql, Object... values) throws Exception;

	public abstract <T> T find(Class<T> clazz, String whereSql, Object... values) throws Exception;

	public abstract <T> T findBySql(Class<T> clazz, String sql, Object... values) throws Exception;

	public abstract int save(Object o) throws Exception;
	
	public abstract  int save(String tableName, Map<String, Object> fieldsMap) throws Exception;
	
	public abstract  void saveCatch(Object o) throws Exception;
	
	public abstract  int update(Object o) throws Exception;

	public abstract  int update(String tableName, Map<String, Object> updateFields, String whereSql, Object... whereValues) throws Exception;
	
	public abstract  int delete(String sql, Object... values) throws Exception;

	public abstract  <T> List<T> searchAll(Class<T> clazz) throws Exception;

	public abstract  <T> List<T> search(Class<T> clazz, String whereSql, Object... values) throws Exception;

	public abstract  <T> List<T> searchBySql(Class<T> clazz, String sql, Object... values) throws Exception;
	
	public abstract  List<Integer> getIntegerList(String sql, Object... values) throws Exception;
	
	public abstract  List<Integer> getIntegerList(StringBuilder sql, Object... values) throws Exception;
	
	public abstract  List<String> getStringList(StringBuilder sql, Object... values) throws Exception;
	
	public abstract  Integer getInteger(StringBuilder sql, Object... values) throws Exception;

	public abstract  Integer getInteger(String sql, Object... values) throws Exception;
	
	public abstract  Double getDouble(String sql, Object... values) throws Exception;
	
	public abstract  Double getDouble(StringBuilder sql, Object... values) throws Exception;
	
	public abstract  String getSource();

	public abstract  WeaverConnection getConnection() throws SQLException;

	public abstract  PreparedStatement getStatement();

	public abstract  ResultSet getResultSet();

	public abstract  int executeUpdate(String sql, Object... values) throws SQLException;

	public abstract  CustomMap map(StringBuilder sql, Object... whereValues) throws Exception;

	public abstract  String getString(String sql, Object... values) throws Exception;

	public abstract  ResultSetMetaData getMetaData();

	public abstract  String[] getColumnLabels();
	
}